package cn.wolfcode.wolf2w.business.service.impl;


import cn.wolfcode.wolf2w.business.api.RemoteDestinationService;
import cn.wolfcode.wolf2w.business.api.domain.Answer;
import cn.wolfcode.wolf2w.business.api.domain.Destination;
import cn.wolfcode.wolf2w.business.api.domain.Question;
import cn.wolfcode.wolf2w.business.mapper.AnswerContentMapper;
import cn.wolfcode.wolf2w.business.mapper.AnswerMapper;
import cn.wolfcode.wolf2w.business.mapper.QuestionMapper;
import cn.wolfcode.wolf2w.business.query.QuestionQuery;
import cn.wolfcode.wolf2w.business.service.IQuestionService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.context.SecurityContextHolder;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 问答Service业务层处理
 * 
 * @author zhuyaohua
 * @date 2024-10-01
 */
@Service
@Transactional
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements IQuestionService {

    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private AnswerContentMapper answerContentMapper;
    @Autowired
    private RemoteUserInfoService remoteUserInfoService;
    @Autowired
    private RemoteDestinationService remoteDestinationService;

    @Override
    public IPage<Question> queryPage(QuestionQuery qo) {
        System.err.println(qo.getCurrentPage()+":"+qo.getPageSize());
        IPage<Question> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());

        IPage<Question> page1 = lambdaQuery().page(page);
        for (Question question : page1.getRecords()) {
            LambdaQueryWrapper<Answer> queryWrapper =new LambdaQueryWrapper<>();
            queryWrapper.eq(Answer::getQuestionId,question.getId());
            queryWrapper.orderByDesc(Answer::getViewNum);
            List<Answer> answers = answerMapper.selectList(queryWrapper);
            if(answers!=null && answers.size()>0){
                Answer answer = answers.get(0);
                question.setHeadImgUrl(answer.getHeadImgUrl());
                question.setDetailDoubt(answer.getTitle());
                question.setViewNum(answer.getViewNum());
                question.setAuthorName(answer.getAuthorName());
                question.setLevel(answer.getLevel());
            }
        }
        return page1;
    }

    @Override
    public Question selectById(Long id) {

        Question question = baseMapper.selectById(id);
        LambdaQueryWrapper<Answer> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Answer::getQuestionId,id);
        queryWrapper.orderByDesc(Answer::getViewNum);
        List<Answer> answers = answerMapper.selectList(queryWrapper);
        question.setAnswerList(answers);

        return question;
    }

    @Override
    public IPage<Question> queryPageSelf(QuestionQuery qo) {
        Long userId = SecurityContextHolder.getUserId();

        IPage<Question> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .eq(Question::getAuthorId,userId)
                .page(page);
    }

    @Override
    public boolean insert(Question question) {
        Long userId = SecurityContextHolder.getUserId();
        question.setAuthorId(userId);

        R<UserInfo> one = remoteUserInfoService.getOne(userId, SecurityConstants.INNER);
        UserInfo data = one.getData();
        question.setAuthorName(data.getNickname());
        question.setHeadImgUrl(data.getHeadImgUrl());
        question.setLevel(data.getLevel());
        question.setCreateTime(new Date());
        question.setDetailDoubt(question.getContents());
        R<Destination> destinationR = remoteDestinationService.getOne(question.getDestId(), SecurityConstants.INNER);
        String name = destinationR.getData().getName();
        question.setDestinationName(name);
        question.setCount(0L);
        question.setIsAttention(0L);
        question.setIsDraft(0L);
        question.setState(1L);
        question.setViewNum(0L);
        baseMapper.insert(question);
        return true;
    }
}
